package com.yx.backstage.converter;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.backstage.model.BookMessage;
import com.yx.backstage.model.param.BookMessageParam;
import com.yx.backstage.model.vo.BookMessageVo;
import com.yx.backstage.util.TimeFormat;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author YangXiong
 */
@Component
public class BookMessageConverter {
    public static List<BookMessageVo> handleResult(Page<BookMessage> page) {
        List<BookMessage> records = page.getRecords();
        List<BookMessageVo> result = new ArrayList<>();
        records.forEach(record->{
            BookMessageVo bookMessageVo = getBookMessageVo(record);
            result.add(bookMessageVo);
        });
        return result;
    }
    public static BookMessageVo getBookMessageVo(BookMessage record) {
        BookMessageVo bookMessageVo = new BookMessageVo();
        bookMessageVo.setId(record.getId());
        bookMessageVo.setName(record.getName());
        bookMessageVo.setAuthor(record.getAuthor());
        bookMessageVo.setKind(record.getKind());
        bookMessageVo.setIntroduction(record.getIntroduction());
        bookMessageVo.setLastChapter(record.getLastChapter());
        bookMessageVo.setStatus(record.getStatus());
        bookMessageVo.setSize(record.getSize());
        bookMessageVo.setType(record.getType());
        bookMessageVo.setEnable(record.getEnable());
        bookMessageVo.setUrl(record.getUrl());
        String lastUpdateTime = DateUtil.format(record.getLastUpdateTime(), TimeFormat.TIME_FORMAT);
        bookMessageVo.setLastUpdateTime(lastUpdateTime);
        return bookMessageVo;
    }

    public static BookMessage handleRequestParam(BookMessageParam param) {
        BookMessage bookMessage = new BookMessage();
        bookMessage.setId(param.getId());
        bookMessage.setStatus(param.getStatus());
        bookMessage.setAuthor(param.getAuthor());
        bookMessage.setIntroduction(param.getIntroduction());
        bookMessage.setKind(param.getKind());
        bookMessage.setType(param.getType());
        bookMessage.setSize(param.getSize());
        bookMessage.setEnable(param.getEnable());
        bookMessage.setName(param.getName());
        bookMessage.setLastChapter(param.getLastChapter());
        String lastUpdateTime = param.getLastUpdateTime();
        bookMessage.setLastUpdateTime(DateUtil.parse(lastUpdateTime));
        return bookMessage;
    }
}
